package arraylist;
import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		ArrayList<String>a=new ArrayList<String>();
		String b,c,d;
		while(in.hasNext())
		{
			while(in.hasNext())
			{
				b=in.next();
			    if (b.endsWith("!!end!!"))break;
			    a.add(b);
			}
			a.add(0, "begin");
			a.add("end");
			System.out.println(a);
			c=in.next();
			System.out.println(a.contains(c));
			System.out.println(a.indexOf(a));
			System.out.println(a.lastIndexOf(a));
			System.out.println(a.get(0));
			a.remove(0);
			System.out.println(a);
			String str=in.next();
			a.set(1, str);
			System.out.println(a);
			ArrayList<String>a1=new ArrayList<String>();
			String str1=in.next();
	        for(int i=0;i<a.size();i++)
			{
				d=a.get(i);
				if(d.contains(str1))
					a1.add(d);
			}
			System.out.println(a1);
			a.remove(str1);
			System.out.println(a);
			a.clear();
			System.out.print(a+" ");
			System.out.print(a.size()+",");
			System.out.println(a.isEmpty());
		}
        in.close();
	}

}
